class Solution {
    public int strToInt(String str) {
        if(str == null || str.trim().equals("")){
            return 0;
        }
        char[] arr = str.trim().toCharArray();
        boolean isNegative = false;
        int i = 0;
        if(arr[i] == '+'){
            i++;
        } else if(arr[i] == '-'){
            i++;
            isNegative = true;
        }
        int res = 0;
        for(; i < arr.length; i++){
            if(arr[i] < '0' || arr[i] > '9'){
                break;
            }
            int boundary = (Integer.MAX_VALUE - (arr[i] - '0')) / 10;
            if((boundary < res) || (boundary == res && arr[i] > '7')){
                return isNegative ?  Integer.MIN_VALUE : Integer.MAX_VALUE;
            }
            res = (arr[i] - '0') + res * 10;
        }
        return isNegative ? -res : res;
    }

    public static void main(String[] args) {
        System.out.println(Integer.MAX_VALUE);
        System.out.println(Integer.MIN_VALUE);
        Solution s = new Solution();
        System.out.println(s.strToInt("+"));
        System.out.println(s.strToInt("+-2"));


        int ans1 = s.strToInt("4193 with words");
        int ans2 = s.strToInt("words and 987");
        int ans3 = s.strToInt("-91283472332");
        int ans4 = s.strToInt("   -42");
        int ans5 = s.strToInt("42");
        System.out.println(ans1);
        System.out.println(ans2);
        System.out.println(ans3);
        System.out.println(ans4);
        System.out.println(ans5);
    }
}